package cn.org.eagle_framework.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @Copyright @ 2013 All right reserved
 * @version 创建时间：Created on 2013-3-7
 * @author 作者：Create by www.360buyli.com
 * @Email: 360buyli@gmail.com
 * @description 用于追加xml文件
 * 关于追加xml文件总共有两步:
 * 1.删除原来的数据
 * 2.追加数据值
 */

public class XmlBiz {

	
	public static void dealXmlBiz(String fileName,String elementName,String attrName,String attrValue,boolean isSpring){
		// 获取读取器
		SAXReader reader = new SAXReader();
		Document doc = null;
		String currPath="resource\\"+fileName;
		try {
			doc = reader.read(new File(currPath));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		// 获取根元素
		Element root = doc.getRootElement();
		if(isSpring){
//			Namespace name=new Namespace("xmlns", "http://www.springframework.org/schema/beans");
//			root.remove(name);
			root.addNamespace("", "http://www.springframework.org/schema/beans");
			
		}
		//删除原来的节点
		List elements = root.elements();
		for (Object object : elements) {
			Element element =(Element) object;
			String curattrValue=element.attributeValue(attrName);
			if(attrValue.equals(curattrValue)){
				root.remove(element);
			}
		}
		Element currentElement = DocumentHelper.createElement(QName.get(elementName, root.getNamespace()));
		currentElement.addAttribute(attrName,attrValue);
		root.add(currentElement);
	
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 指定XML编码
		try {
			format.setEncoding("utf-8");
			XMLWriter writer = new XMLWriter(new FileWriter(currPath),format);
			writer.write(doc);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch blockbasepackage
			e.printStackTrace();
		}
	}
	public static void appendSpringConf(GeneratorBiz generatorBiz,String className) {
		String basepackage=generatorBiz.getBasepackage();
		 basepackage=basepackage+"."+className.toLowerCase()+"."+"config"+".";
		 basepackage=basepackage.replaceAll("\\.", "/");
		 String path=basepackage+className+"_spring.xml";
		 System.out.println("spring paht:"+path);
		dealXmlBiz("applicationContext.xml","import","resource",path,true);
	}

	public static void appendStruts2Conf(GeneratorBiz generatorBiz,String className) {
		String basepackage=generatorBiz.getBasepackage();
		 basepackage=basepackage+"."+className.toLowerCase()+"."+"config"+".";
		 basepackage=basepackage.replaceAll("\\.", "/");
		 String path=basepackage+className+"_struts.xml";
		dealXmlBiz("struts.xml","include","file",path,false);
	}

	public static void appendIbatisConf(GeneratorBiz generatorBiz,String className) {
		String basepackage=generatorBiz.getBasepackage();
		 basepackage=basepackage+"."+className.toLowerCase()+"."+"domain"+".";
		 basepackage=basepackage.replaceAll("\\.", "/");
		 String path=basepackage+className+".xml";
		dealXmlBiz("sqlmapconfig.xml","sqlMap","resource",path,false);
	}

}
